<?php
class PublicAction extends BaseAction
{
    /**
     * 执行登陆操作
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function doLogin($email, $passwd)
    {

    }

    /**
     * 用户登陆界面
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function login()
    {
        if ($this->getLoginUserInfo()) {
            redirect(U('/'));
        }

        $this->display();
    }

    public function loginDo()
    {
        if (isPost()) {
            $email = h(getPost('email'));
            $pwd = md5(getPost('pwd'));
            $this->autoCheckToken();
            $user = $this->checkLogin($email, $pwd);
            if ($user) {
                $uid = $user['id'];
                $this->loginLog($uid); //记录登陆日志
                $this->loginUrl($uid);
                $this->success('登陆成功！');
            } else {
                $this->error('登陆失败！');
            }
        }
    }

    /**
     * 检查登录信息是否正确
     * @param String $email 用户登录邮箱
     * @param String $pas 用户密码
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function checkLogin($email, $pwd = null)
    {
        $user = array();
        $map = array();
        $UserModel = M('User');
        $field = 'id,email,status';
        if ($email && $pwd) {
            $map['email'] = $email;
            $map['passwd'] = $pwd;
        } else if ($email) {
            $map['email'] = $email;
        } else {
            return false;
        }
        $user = $UserModel->where($map)->field($field)->find();
        if ($user) {
            return $user;
        }
        return false;
    }

    public function loginOut()
    {
        unset($_SESSION);
        Session::destroy();
        Cookie::clear();
        redirect(U('/'));
    }
}

?>